package com.ahtcm.tcmlearning.service.iml;

import com.ahtcm.tcmlearning.dao.*;
import com.ahtcm.tcmlearning.dataObject.brushProblem.Choice;
import com.ahtcm.tcmlearning.dataObject.brushProblem.QuestionToShow;
import com.ahtcm.tcmlearning.dataObject.brushProblem.RateOfLearning;
import com.ahtcm.tcmlearning.dataObject.brushProblem.SubjectChapterParent;
import com.ahtcm.tcmlearning.enums.QuestionRecordTypeEnum;
import com.ahtcm.tcmlearning.enums.QuestionTypeEnum;
import com.ahtcm.tcmlearning.model.*;
import com.ahtcm.tcmlearning.service.BrushProblemService;
import com.ahtcm.tcmlearning.utils.brushProblem.AnswerOrChoiceToValueUtil;
import com.ahtcm.tcmlearning.utils.brushProblem.RateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author yangrongdeng
 * @descriptionn 刷题模块实现类
 * @date 2019.01.06
 */
@Service
public class BrushProblemServiceIml implements BrushProblemService {
    private static final int questionCount = 20;
    private static final int isValid = 1;
    private static  final String tipOne="此功能模块未开放";
    private static LinkedList<BaseDictionary> baseDictionaryList;
    @Autowired
    BaseDictionaryMapper baseDictionaryMapper;
    @Autowired
    QuestionRecordMapper questionRecordMapper;
    @Autowired
    QuestionMapper questionMapper;
    @Autowired
    CollectionMapper collectionMapper;
    @Autowired
    MedicineMapper medicineMapper;
    @Autowired
    PrescriptionMapper prescriptionMapper;

    //按学科返回章节内容列表
    @Override
    public List<SubjectChapterParent> subjectChapterList(Integer subjectId) {
        List<BaseDictionary> dbList = baseDictionaryMapper.subjectChapterList(subjectId);
        List<BaseDictionary> parentList = getParent(dbList, subjectId);
        List<BaseDictionary> childList = getChild(dbList, subjectId);
        List<SubjectChapterParent> list = new ArrayList<>();
        System.out.println(list);
        parentList.stream().forEach(parent -> {
            SubjectChapterParent subjectChapterParent = new SubjectChapterParent();
            BeanUtils.copyProperties(parent, subjectChapterParent);
            list.add(subjectChapterParent);
        });
        for (SubjectChapterParent parent : list) {
            List<BaseDictionary> child = new ArrayList<>();
            for (BaseDictionary baseDictionary : childList)
                if (baseDictionary.getParentId().equals(parent.getDicId())) child.add(baseDictionary);
            parent.setChild(child);
        }
        return list;
    }

    //辅助函数获得次级父类
    private List<BaseDictionary> getParent(List<BaseDictionary> dbList, Integer subjectId) {
        List<BaseDictionary> parentList = new ArrayList<>();
        for (BaseDictionary baseDictionary : dbList) {
            if (baseDictionary.getParentId().equals(subjectId)) {
                parentList.add(baseDictionary);
            }
        }
        return parentList;
    }

    //辅助函数：获得子类
    private List getChild(List<BaseDictionary> dbList, Integer subjectId) {
        List<BaseDictionary> childList = new ArrayList<>();
        List<BaseDictionary> parentList = getParent(dbList, subjectId);
        dbList.removeAll(parentList);
        childList = dbList;
        return childList;
    }

    //按类目从数据库获得题目
    @Override
    public List<QuestionToShow> findQuestionByCategory(Integer userId, Integer categoryId) {
        Integer beginQuestionId = (Integer) questionRecordByUserId(categoryId, userId, QuestionRecordTypeEnum.NORMAL_QUESTION_RECORD.getCode());
        System.out.println("用户-" + userId + "-需要从类别为：" + categoryId + "的第" + beginQuestionId + "的后一题开始刷题");
        List<Question> dbList = questionMapper.getQuestion(categoryId, beginQuestionId, questionCount);
        List<QuestionToShow> list = new LinkedList<>();
        for (Question question : dbList) {
            QuestionToShow questionToShow = new QuestionToShow();
            BeanUtils.copyProperties(question, questionToShow);
            List<Choice> choiceList = new LinkedList<>();
            if (question.getChoicea() != null)
                choiceList.add(new Choice(question.getChoicea(), AnswerOrChoiceToValueUtil.choiceToValue(question.getChoicea())));
            if (question.getChoiceb() != null)
                choiceList.add(new Choice(question.getChoiceb(), AnswerOrChoiceToValueUtil.choiceToValue(question.getChoiceb())));
            if (question.getChoicec() != null)
                choiceList.add(new Choice(question.getChoicec(), AnswerOrChoiceToValueUtil.choiceToValue(question.getChoicec())));
            if (question.getChoiced() != null)
                choiceList.add(new Choice(question.getChoiced(), AnswerOrChoiceToValueUtil.choiceToValue(question.getChoiced())));
            if (question.getChoicee() != null)
                choiceList.add(new Choice(question.getChoicee(), AnswerOrChoiceToValueUtil.choiceToValue(question.getChoicee())));
            questionToShow.setChoiceList(choiceList);
            if (question.getQuestionType() != 2 || !question.getQuestionType().equals("2")) {
                questionToShow.setAnswerToShow(AnswerOrChoiceToValueUtil.answerToValue(question.getAnswer()));
            }
            if (question.getQuestionType().equals(QuestionTypeEnum.SINGLE_ANSWER_QUESTION.getCode()))
                questionToShow.setTypeToShow(QuestionTypeEnum.SINGLE_ANSWER_QUESTION.getMessage());
            if (question.getQuestionType().equals(QuestionTypeEnum.MULTIPLE_ANSWER_QUESTION.getCode()))
                questionToShow.setTypeToShow(QuestionTypeEnum.MULTIPLE_ANSWER_QUESTION.getMessage());
            if (question.getQuestionType().equals(QuestionTypeEnum.JUDGE_QUESTION.getCode()))
                questionToShow.setTypeToShow(QuestionTypeEnum.JUDGE_QUESTION.getMessage());

            list.add(questionToShow);
        }
        return list;
    }


    //向用户做题记录表中添加记录
    @Override
    @Transactional
    public void addQuestionRecord(Integer questionType, Integer userId, Integer categoryId, Integer questionId, Integer isRight) {
        Date createTime = new Date();
        questionRecordMapper.addQuestionRecord(userId, questionId, isRight, createTime, isValid, categoryId, questionType,null);
        System.out.println("添加成功");
        return;
    }

    //向题目收藏表中添加记录
    @Override
    @Transactional
    public void collectionQuestion(Integer targetId, Integer userId, Integer collectionType, Integer dicId) {
        Date createTime = new Date();
        collectionMapper.collectionQuestion(targetId, userId, collectionType, dicId, createTime, isValid);
        System.out.println("添加成功");

    }

    /**
     * @param categoryId   类目id
     * @param userId       用户id
     * @param questionType 0是章节模块刷题的内容
     * @description 查询用户章节做题做到哪一题
     */
    private Integer questionRecordByUserId(Integer categoryId, Integer userId, Integer questionType) {
        if (questionType.equals("0") || questionType == 0) {
            //三种情况，1：为未开始刷为null，2：刷完了此类型，，3：正常记录
            QuestionRecord record = questionRecordMapper.chapterQuestionRecord(categoryId, userId);
            Question theLastQuestionId = questionMapper.theLastQuestionByCategory(categoryId);
            if (theLastQuestionId != null)
                return record == null ? 0 : (record.getQuestionId() == theLastQuestionId.getQuestionId() ? 0 : record.getQuestionId());
        }
        return null;
    }


//    @Override
//    public Object getKnowledgePointDetail(Integer functionType, Integer userId) {
//        QuestionRecord userDegree = questionRecordMapper.degreeByQuestionType(functionType, userId);
//        if (functionType.equals(QuestionRecordTypeEnum.MEDICINE_MODULAR__RECORD.getCode())) {
//            List<Medicine> medicineList = medicineMapper.selectAllFromMedicine();
//            if (medicineList.size()==0)return tipOne;
//            if (userDegree==null)return medicineList.get(0);
//                    Medicine nowBrush = new Medicine();
//            for (Medicine medicine : medicineList) {
//                if (medicine.getMedicineId().equals(userDegree.getDicId()))
//                    nowBrush = medicine;
//            }
//            int index = medicineList.indexOf(nowBrush);
//            return nowBrush.getMedicineId().equals(medicineList.get(medicineList.size() - 1).getMedicineId()) ? medicineList.get(0) : medicineList.get(index + 1);
//        }
//        if (functionType.equals(QuestionRecordTypeEnum.PRESCRIPTION.getCode())) {
//            List<Prescription> prescriptionList = prescriptionMapper.selectAllFromPrescription();
//            if (prescriptionList.size()==0)return tipOne;
//            if (userDegree==null)return prescriptionList.get(0);
//            Prescription nowBrush = new Prescription();
//            for (Prescription prescription : prescriptionList) {
//                if (prescription.getPrescriptionId().equals(userDegree.getDicId()))
//                    nowBrush = prescription;
//            }
//            int index = prescriptionList.indexOf(nowBrush);
//            return nowBrush.getPrescriptionId().equals(prescriptionList.get(prescriptionList.size() - 1).getPrescriptionId()) ? prescriptionList.get(0) : prescriptionList.get(index + 1);
//        }
//
//return  null;
//    }
//
//
//
//    private BaseDictionary getBrushCategory(List<BaseDictionary> subjectList, QuestionRecord userDegree, Integer functionType) {
//        List<BaseDictionary> brushChildCategoryList = new ArrayList<>();//存储一类的方剂
//        Integer parentId = null;//主类型id
//        BaseDictionary degree = null;//进度
//        List<BaseDictionary> parentList = getParent(subjectList, functionType);
//        List<BaseDictionary> childList = getChild(subjectList, functionType);
//        for (BaseDictionary child : childList) {
//            if (userDegree.getDicId().equals(child.getDicId())) degree = child;
//        }
//        parentId = degree.getParentId();
//        for (BaseDictionary child : childList) {
//            if (child.getParentId().equals(parentId))
//                brushChildCategoryList.add(child);
//        }
//        if (degree.getOrderNum().equals(brushChildCategoryList.get(brushChildCategoryList.size() - 1).getOrderNum())) {
//            System.out.println("已经刷到最后一中了");
//        }
//        int index = brushChildCategoryList.indexOf(degree);
//        System.out.println("索引为：" + brushChildCategoryList.indexOf(degree));
//
//        System.out.println("brushChildCategoryList:" + brushChildCategoryList);
//        return brushChildCategoryList.get(index + 1);
//
////
////        }else {
////            //总list也为空要考虑,做题记录为空也要考率
////            BaseDictionary brushCategory=getParent(subjectList,functionType).get(0);
////        }
////
////return  null;
//    }




}
